package com.wmb.userservice.controller;


import com.wmb.userservice.aop.AutoFill;
import com.wmb.userservice.aop.OperationType;
import com.wmb.userservice.model.dto.LoginUserDTO;
import com.wmb.userservice.model.dto.UserPageDTO;
import com.wmb.userservice.model.pojo.User;
import com.wmb.userservice.model.vo.LoginUserVO;
import com.wmb.userservice.service.UserService;
import com.wmb.userservice.utils.PageResult;
import com.wmb.userservice.utils.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private UserService userService;


    /**
     * 登录
     * @param loginUserDTO
     * @return
     */
    @AutoFill(value = OperationType.LOGIN)
    @PostMapping("/login")
    public Result<LoginUserVO> login(@RequestBody LoginUserDTO loginUserDTO) {
        return userService.login(loginUserDTO);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @AutoFill(value = OperationType.REGISTER)
    @PostMapping("/register")
    public Result<User> register(@RequestBody User user) {
        return userService.register(user);
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @AutoFill(value = OperationType.SELECT)
    @GetMapping("/{userId}")
    public Result<User> getUser(@PathVariable Long userId) {
        return userService.getUserById(userId);
    }

    /**
     * 分页获取用户列表
     * @param userPageDTO
     * @return
     */
    @AutoFill(value = OperationType.SELECT)
    @GetMapping("/page-list")
    public Result<PageResult> pageList(@RequestParam UserPageDTO userPageDTO) {
        return userService.pageQuery(userPageDTO);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @AutoFill(value = OperationType.UPDATE)
    @PutMapping("/update")
    public Result<User> update(@RequestBody User user) {
        return userService.updateUser(user);
    }


    /**
     * 重置密码
     * @param userId
     * @return
     */
    @AutoFill(value = OperationType.UPDATE)
    @PostMapping("/reset-password")
    public Result<User> reset(@RequestParam Long userId) {
        return userService.resetPassword(userId);
    }


    /**
     * 修改用户权限 -- superAdmin专用
     * @param userId
     * @param userNewRoleId
     * @return
     */
    @AutoFill(value = OperationType.UPDATE)
    @PutMapping("/user/alter-permission")
    public Result alterUserPermission(Long userId, String userNewRoleId) {
        return userService.alterUserPermissionByUserId(userId, userNewRoleId);
    }
}
